$(document).ready(function() {
	$("#demo").validate({
		rules:{
			"major.majorGroupId": {
				required: true	    		
			},
			"major.majorName": {
				required: true,
				maxlength:30
			},
			"major.majorRemark": {
				required: false,
	    		maxlength: 500
			},
			"major.departmentId": {
				required: true
			}		
		}
	});
});

//返回department的json数据，带被选择数据
function queryDepartmentList(departmentId){
	$.post(
		"../AjaxResultData/listAllDepartment.action",
		function(data){
			$("#departmentId").empty();
			$("#departmentId").append("<option value=''>--未选择--</option>");
			var departmentData = eval("("+data.departmentData+")");  
			for(var i = 0;i<departmentData.length;i++){
				if(departmentId==departmentData[i].departmentId){
					$("#departmentId").append("<option value='"+departmentData[i].departmentId+"' selected='selected'>"+departmentData[i].departmentName+"</option>");
				}else{
					$("#departmentId").append("<option value='"+departmentData[i].departmentId+"'>"+departmentData[i].departmentName+"</option>");
				}
			}
		},
			"json"
	);
}

//根据DepartmentId返回majorGroup的json数据
function queryMajorGroupByDepartmentId(departmentId,majorGroupId){
	$.post(
		"../AjaxResultData/listMajorGroupByDepartmentId.action",
		{"majorGroup.departmentId":departmentId},
		function(data){
			$("#majorGroupId").empty();
			$("#majorGroupId").append("<option value=''>--未选择--</option>");
			var majorGroupData = eval("("+data.majorGroupDataByDepartmentId+")");
			for(var i = 0; i<majorGroupData.length; i++){
				if(majorGroupId==majorGroupData[i].majorGroupId){
					$("#majorGroupId").append("<option value='"+majorGroupData[i].majorGroupId+"' selected='selected'>"+majorGroupData[i].majorgroupName+"</option>");	
				}else{
					$("#majorGroupId").append("<option value='"+majorGroupData[i].majorGroupId+"'>"+majorGroupData[i].majorgroupName+"</option>");
				}
			}
		}
	);
}

function createDepartmentChange(majorGroupId){
	$("#departmentId").change(function(){
		var departmentId = document.getElementById("departmentId").value;
		$("#majorGroupId").empty();
		queryMajorGroupByDepartmentId(departmentId,majorGroupId);
	});
}

function initForm(departmentId,majorGroupId){
	queryDepartmentList(departmentId);
	queryMajorGroupByDepartmentId(departmentId,majorGroupId);
}